package com.youkeyi.ddy.cloud.basic.crm.doctor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.doctor.entity.TblDoctor;
import com.youkeyi.ddy.cloud.basic.crm.doctor.service.TblDoctorService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.CrmDoctorCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.dto.CrmDoctorListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.dto.CrmDoctorListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.param.CrmDoctorCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.param.CrmDoctorDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.param.CrmDoctorQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.param.CrmDoctorUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-12
 */
@Service
public class TblDoctorController implements CrmDoctorCloudService {

    @Autowired
    private TblDoctorService tblDoctorService;

    @Override
    public CrmDoctorListCloudDto query(CrmDoctorQueryCloudParam param) {
        CrmDoctorListCloudDto dto = new CrmDoctorListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getDoctorPrimaryKeys())) {
            param.getDoctorPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCodes())) {
            param.getCodes().removeAll(Collections.singleton(null));
        }

        QueryWrapper<TblDoctor> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getDoctorPrimaryKeys()), TblDoctor::getPrimaryKey, param.getDoctorPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), TblDoctor::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCodes()), TblDoctor::getCode, param.getCodes())
                .like(StringUtils.isNotBlank(param.getNameFuzzy()), TblDoctor::getName, param.getNameFuzzy())
                .like(StringUtils.isNotBlank(param.getPhoneFuzzy()), TblDoctor::getPhone, param.getPhoneFuzzy())
                .ne(TblDoctor::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<TblDoctor> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<TblDoctor> doctorList = tblDoctorService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(doctorList.getRecords())) {
            dto.setTotal(doctorList.getTotal());

            List<CrmDoctorListItemCloudDto> items = new ArrayList<>();
            doctorList.getRecords().forEach(doctor -> {
                CrmDoctorListItemCloudDto item = new CrmDoctorListItemCloudDto();
                item.setDoctorPrimaryKey(doctor.getPrimaryKey());
                item.setCorpKey(doctor.getCorpKey());
                item.setCode(doctor.getCode());
                item.setName(doctor.getName());
                item.setPhone(doctor.getPhone());
                item.setAddress(doctor.getAddress());
                item.setDesc(doctor.getDesc());
                item.setRemark(doctor.getRemark());
                item.setIsDeleted(doctor.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(doctor.getCreateTime() != null ? doctor.getCreateTime().getTime() : null);
                item.setCreateUserKey(doctor.getCreateUserKey());
                item.setCreateUserName(doctor.getCreateUserName());
                item.setUpdateTime(doctor.getUpdateTime() != null ? doctor.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(doctor.getUpdateUserKey());
                item.setUpdateUserName(doctor.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmDoctorCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        TblDoctor doctor = new TblDoctor();
        doctor.setPrimaryKey(SimpleUUidUtils.getUuid());
        doctor.setCorpKey(param.getCorpKey());
        doctor.setCode(param.getCode());
        doctor.setName(param.getName());
        doctor.setPhone(param.getPhone());
        doctor.setAddress(param.getAddress());
        doctor.setDesc(param.getDesc());
        doctor.setRemark(param.getRemark());
        doctor.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        doctor.setCreateUserKey(param.getCreateUserKey());
        doctor.setCreateUserName(param.getCreateUserName());
        doctor.setCreateTime(new Date());
        boolean save = tblDoctorService.save(doctor);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(doctor.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmDoctorUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getDoctorPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<TblDoctor> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(TblDoctor::getPrimaryKey, param.getDoctorPrimaryKey());
        TblDoctor doctor = new TblDoctor();
        doctor.setPrimaryKey(param.getDoctorPrimaryKey());
        doctor.setCode(param.getCode());
        doctor.setName(param.getName());
        doctor.setPhone(param.getPhone());
        doctor.setAddress(param.getAddress());
        doctor.setDesc(param.getDesc());
        doctor.setRemark(param.getRemark());
        doctor.setUpdateUserKey(param.getUpdateUserKey());
        doctor.setUpdateUserName(param.getUpdateUserName());
        doctor.setUpdateTime(new Date());
        boolean save = tblDoctorService.update(doctor, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmDoctorDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getDoctorPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<TblDoctor> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(TblDoctor::getPrimaryKey, param.getDoctorPrimaryKeys());

        TblDoctor doctor = new TblDoctor();
        doctor.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        doctor.setUpdateUserKey(param.getUpdateUserKey());
        doctor.setUpdateUserName(param.getUpdateUserName());
        doctor.setUpdateTime(new Date());
        boolean save = tblDoctorService.update(doctor, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
